/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package biofilter.records.NimbleGen;

import java.util.*;
import java.io.*;
import biofilter.records.*;
import biofilter.exceptions.InvalidRecordException;
import biofilter.util.StringUtil;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Normalized data sets are expected to have the following form:
 * PROBE_ID        t2_1_Cy3 loess normalized       t2_2_Cy3 loess normalized       t2_3_Cy3 loess normalized       t3_1_Cy3 loess normalized       t3_2_Cy3 loess normalized       t3_3_Cy3 loess normalized
 * 1423371-1424636_1060    11.09256        12.56865        11.30908        11.75166        12.01154        11.82326
 *
 * @author Daniel Quest
 */
public class NimbleGenNormalizedRecord extends RecordBase {

    public NimbleGenNormalizedRecord(){

    }

    private String probeID = "";

    public String getProbeID() {
        return probeID;
    }

    public void setProbeID(String probeID) {
        this.probeID = probeID;
    }

    
    /*
     * Parse an entire normalized file
     */
/*    public NimbleGenNormalizedRecord parseFile(String fileNamePath){
        try{
         boolean firstLine = true;
         LinkedList ll = null;
         BufferedReader in = new BufferedReader(new FileReader(fileNamePath));
         String line = "";
         while ((line = in.readLine()) != null) {
             if(firstLine){ this.parseHeader(line); }
             else {         ll = this.parse(line);  }
         }
        }catch (java.io.FileNotFoundException fnf){
            System.err.println("File not found exception: " + fnf);
        }catch (java.io.IOException io){
            System.err.println("IO Exception: " + io);
        }
        return this;
    }
 */
    /*
     * parse the header for the file.
     * headers are expected to look as follows:
     * PROBE_ID        t2_1_Cy3 loess normalized       t2_2_Cy3 loess normalized       t2_3_Cy3 loess normalized       t3_1_Cy3 loess normalized
       t3_2_Cy3 loess normalized       t3_3_Cy3 loess normalized
     */
     LinkedList experementalConditions = null;
     public void parseHeader(String headerLine) throws IOException{
        String [] tokens = headerLine.split("\\s");
        if ( !tokens[0].startsWith("PROBE_ID") ) throw new IOException("Header does not start with PROBE_ID in Normalized NimbleGen file, it starts with: \'" + tokens[0] + "\'");
        if ( tokens.length < 2 ) throw new IOException("Header does not contain enough experement descriptions!");
        experementalConditions = new LinkedList();
        for(int i = 1; i< tokens.length; i++){
            experementalConditions.add(tokens[i]);
        }
     }


    public LinkedList getValues(){
        return probeValues;
    }
    public String get(){ return record; }
    public void set(String s){ 
        try {
            this.record = s;
            parse(s);
        } catch (InvalidRecordException ex) {
            Logger.getLogger(NimbleGenNormalizedRecord.class.getName()).log(Level.SEVERE, null, ex);
        }
    }  //again initialization is not lazy ... may want to fix this at a later date.
    LinkedList probeValues = null;
    String record;
    /*
     * parse one line of a file
     */
    public void parse(String line) throws InvalidRecordException {
        record = line;
        LinkedList ll = new LinkedList();
        probeValues = new LinkedList();
        Double dd = new Double("0");
        String [] tokens = line.split("\\s");
        if(tokens.length < 7){
            throw new InvalidRecordException("Records.NimbleGen.NimbleGenNormalizedRecord: " + line);
        }
        for(int i = 0; i<tokens.length; i++){
            if( tokens[i].equalsIgnoreCase("") ){
                ;
            }else{
                ll.add(tokens[i] );
                //System.err.println(tokens[i]);
            }            
        }
        //System.err.println(ll.size());
        probeID = (String) ll.get(0);
        for(int i = 1; i<ll.size(); i++){
            probeValues.add(dd.parseDouble((String)ll.get(i)));
        }

        //System.err.println(probeValues.size());
    }
/*
    public Double get(int indx){
        if (probeValues.size() < indx) return 0.0;
        else return (Double) probeValues.get(indx);
    }
 */

}
